//
//  ShareUMView.swift
//  ios_swift_demo1
//
//  Created by liutao on 2020/7/31.
//  Copyright © 2020 lt. All rights reserved.
//

import UIKit

class BottomShareView: UIView {
    public var saveVideoBtnTouch: (() -> Void)?
        
    public var weatchBtnTouch: (() -> Void)?
        
    public var friendCircleBtnTouch: (() -> Void)?
    
    public var cancelBtnTouch: (() -> Void)?
    
        
    override init(frame: CGRect) {
            
        super.init(frame: frame)
        sudokuConstraints()
            
    }
       
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
        
    //MARK: - 布局
    func sudokuConstraints(){
        let layerView = UIView()
        layerView.frame = CGRect(x: 0, y: 0, width:screenWidth, height: (178.5/667)*screenHeight)
        layerView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
        let layer = CALayer()
        layer.frame = layerView.bounds
        layer.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1).cgColor
        layerView.layer.addSublayer(layer)
        layerView.layer.cornerRadius = 10;
        layerView.layer.masksToBounds = true
        self.addSubview(layerView)
        
        addTitleLabel(layerView)
        addSaveVideoBtn(layerView)
        addWeatchBtn(layerView)
        addFriendCircleBtn(layerView)
        addHorizontalDiver(layerView)
        addCancelBtn(layerView)
     }
          
      
    func addTitleLabel(_ view:UIView){
        let label = UILabel()
        let attrString = NSMutableAttributedString(string: "选择想要的分享方式")
        label.frame = CGRect(x:0, y: 14, width: screenWidth, height: 15)
        label.textAlignment = .center
        label.numberOfLines = 0
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang-SC-Regular", size: 13)!,.foregroundColor: UIColor(red: 0.2, green: 0.2, blue: 0.2,alpha:1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        label.attributedText = attrString
        view.addSubview(label)
    }
    
    func addSaveVideoBtn(_ view:UIView){
        let containerView = UIView()
        containerView.isUserInteractionEnabled = true
        containerView.frame = CGRect(x: (39/375)*screenWidth, y: (44.5/667)*screenHeight, width: (60/375)*screenWidth, height: (70/375)*screenWidth)
        view.addSubview(containerView)
        
        let imageView = UIImageView(image: UIImage(named: "icon_save_video"))
        imageView.frame = CGRect(x: (containerView.frame.width-(48/375)*screenWidth)/2, y: 0, width: (48/375)*screenWidth, height: (48/375)*screenWidth)
        containerView.addSubview(imageView)
        
        let label = UILabel()
        let attrString = NSMutableAttributedString(string: "保存视频")
        label.frame = CGRect(x: 0, y: (50/375)*screenWidth, width: (60/375)*screenWidth, height: 11.5)
        label.numberOfLines = 0
        label.textAlignment = .center
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang-SC-Regular", size: 12)!,.foregroundColor: UIColor(red: 0.2, green: 0.2, blue: 0.2,alpha:1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        label.attributedText = attrString
        containerView.addSubview(label)
        let tap = UITapGestureRecognizer(target: self, action: #selector(setSaveVideoBtnTouch))
        containerView.addGestureRecognizer(tap)
    }
    
   @objc func setSaveVideoBtnTouch(){
        saveVideoBtnTouch?()
    }
    
    func addWeatchBtn(_ view:UIView){
        let containerView = UIView()
        containerView.isUserInteractionEnabled = true
        containerView.frame = CGRect(x: (screenWidth-(48/375)*screenWidth)/2, y: (44.5/667)*screenHeight, width: (60/375)*screenWidth, height: (70/375)*screenWidth)
        view.addSubview(containerView)
       
        let imageView = UIImageView(image: UIImage(named: "icon_weatch"))
        imageView.frame = CGRect(x: (containerView.frame.width-(48/375)*screenWidth)/2, y: 0, width: (48/375)*screenWidth, height: (48/375)*screenWidth)
        containerView.addSubview(imageView)
       
        let label = UILabel()
        let attrString = NSMutableAttributedString(string: "发给朋友")
        label.frame = CGRect(x: 0, y: (50/375)*screenWidth, width: (60/375)*screenWidth, height: 11.5)
        label.numberOfLines = 0
        label.textAlignment = .center
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang-SC-Regular", size: 12)!,.foregroundColor: UIColor(red: 0.2, green: 0.2, blue: 0.2,alpha:1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        label.attributedText = attrString
        containerView.addSubview(label)
        let tap = UITapGestureRecognizer(target: self, action: #selector(setWeatchBtnTouch))
        containerView.addGestureRecognizer(tap)
       }
    
    @objc func setWeatchBtnTouch(){
           weatchBtnTouch?()
       }
    
    func addFriendCircleBtn(_ view:UIView){
        let containerView = UIView()
        containerView.isUserInteractionEnabled = true
        containerView.frame = CGRect(x: view.frame.maxX-(39/375)*screenWidth-(48/375)*screenWidth, y: (44.5/667)*screenHeight, width: (60/375)*screenWidth, height: (70/375)*screenWidth)
        view.addSubview(containerView)
     
        let imageView = UIImageView(image: UIImage(named: "icon_friend_circle"))
        imageView.frame = CGRect(x: (containerView.frame.width-(48/375)*screenWidth)/2, y: 0, width: (48/375)*screenWidth, height: (48/375)*screenWidth)
        containerView.addSubview(imageView)
     
        let label = UILabel()
        let attrString = NSMutableAttributedString(string: "发朋友圈")
        label.frame = CGRect(x: 0, y: (50/375)*screenWidth, width: (60/375)*screenWidth, height: 11.5)
        label.numberOfLines = 0
        label.textAlignment = .center
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang-SC-Regular", size: 12)!,.foregroundColor: UIColor(red: 0.2, green: 0.2, blue: 0.2,alpha:1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        label.attributedText = attrString
        containerView.addSubview(label)
        let tap = UITapGestureRecognizer(target: self, action: #selector(setFriendCircleBtnTouch))
        containerView.addGestureRecognizer(tap)
    }
    
    @objc func setFriendCircleBtnTouch(){
        friendCircleBtnTouch?()
        print("点击了发朋友圈")
    }
    
    //横线
    func addHorizontalDiver(_ view:UIView){
        let layerView = UIView()
        layerView.frame = CGRect(x: 0, y: (116/667)*screenHeight, width: screenWidth, height: 0.5)
        // layerFillCode
        layerView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
        let layer = CALayer()
        layer.frame = layerView.bounds
        layer.backgroundColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1).cgColor
        layerView.layer.addSublayer(layer)
        view.addSubview(layerView)
    }
    
    func addCancelBtn(_ view:UIView){
        let label = UILabel()
        let attrString = NSMutableAttributedString(string: "取消")
        label.frame = CGRect(x: 0, y: (132/667)*screenHeight, width: screenWidth, height: 13.5)
        label.numberOfLines = 0
        label.textAlignment = .center
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang-SC-Medium", size: 14)!,.foregroundColor: UIColor(red: 0.6, green: 0.6, blue: 0.6,alpha:1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        label.attributedText = attrString
        view.addSubview(label)
        
        label.addGestureRecognizer(UIGestureRecognizer.init(target: self, action: #selector(cancelClick)))
    }
    
   @objc func cancelClick(){
        cancelBtnTouch?()
    }
}
